/*
 *  Copyright 2009 Lucas Nazário dos Santos
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.sourceforge.retriever.sample;

import java.net.MalformedURLException;
import java.net.URL;

import net.sourceforge.retriever.Retriever;
import net.sourceforge.retriever.RetrieverListener;
import net.sourceforge.retriever.Event;

/*
 * This sample shows how users can listen for some crawler events.
 */
public class ListeningForCrawlerEvents {


	public static void main(final String[] args) throws MalformedURLException {
		// Creates the crawler
		final Retriever crawler = new Retriever();

		// Adds listener for crawler events
		crawler.addListener(new RetrieverListener() {
			public void onEvent(final Event event, final Retriever source) {
				if (event == Event.FINISHED) {
					System.out.println("The crawler is done.");
				} else if (event == Event.STARTED) {
					System.out.println("The crawler started.");
				} else if (event == Event.STOPPING) {
					System.out.println("The crawler is stopping.");
				}
			}
		});

		// Adds any seed
		crawler.addSeed(new URL("http://www.google.com/"));

		// Starts the crawler
		new Thread() {
			@Override
			public void run() {
				crawler.start();
			}
		}.start();

		// Waits one second to stop the crawler
		try {
			Thread.sleep(1000);
		} catch (final InterruptedException e) {
		}

		crawler.stop();
	}
}